<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Gupiao2 extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //http://gupiao.test/api/gupiao2/testmath
    //测试三角函数
    function testmath()
    {
        $k1 = ((46.5 - 45.05) / 45.05 - (45.5 - 45.05) / 45.05) * 1000 / 10;
        $k = atan($k1);//斜率=>弧度
        $a = rad2deg($k);//弧度=>角度
        echo $k . "<br> " . $a;
    }


    //追涨杀跌
    //5分钟角度向上 - 分时均线角度 > 60 买
    //分时均线角度 - 5分钟角度向上 > 60 卖
    protected $zdf = 0.02;//涨跌幅 2%
    //http://gupiao.test/api/gupiao2/isbuy
    //是否买
    //1.股价低于分时均线%1一下
    //2.分时均线角度水平
    function isbuy($list)
    {
        $open = $list[0]['close'];//开盘价
        $num = count($list);
        $pj = 0;//平均股价
        $i = 1;
        foreach ($list as &$t) {
            $pj += $t['close'];
            $t['pj'] = $pj / $i;
            $i++;
        }
        $pj = $list[$num-1]['pj']; //之前的平均股价
        $close = $list[$num-1]['close'];//最新价格
        $diefu = ($close - $pj) / $open; //跌幅 %

        //相邻5分钟的走势
        $k1 = (($list[$num-1]['pj'] - $open) / $open - ($list[$num-2]['pj'] - $open) / $open) * 1000 / 10;
        $k = atan($k1);//斜率=>弧度
        $a = rad2deg($k);//弧度=>角度

        $k1 = (($list[$num-1]['close'] - $open) / $open - ($list[$num-2]['close'] - $open) / $open) * 1000 / 10;
        $k = atan($k1);//斜率=>弧度
        $b = rad2deg($k);//弧度=>角度

        if ($diefu < 0 && $b > 10 && $b - $a > 30) {
            echo  "<br>b=". $b ." a=".$a." ".$list[$num-1]['time'];
            $this->addbuy($list[$num-1]);
        } else if ($diefu > 0 && $b < -10 && $a - $b > 30) {
            echo  "<br>b=". $b ." a=".$a." ".$list[$num-1]['time'];
            $this->addsell($list[$num - 1]);
        }

    }

    protected $sbase = 30;//原始股数

    function addsell($hck) {

        $close = $hck['close'];//需要买入的价格
        $up = $close + $close * 0.01;
        $down = $close - $close * 0.01;
        $list = \app\admin\model\stock\Sslog::where('state', 0)->where("s_price < {$up}  and  s_price > {$down}" )->select();
        if ($list && $this->sbase < 4) {
            return;
        }

        $buy = new \app\admin\model\stock\Sslog();
        $buy->s_price = $close;
        $buy->riqi = $hck['riqi'];
        $buy->time = $hck['time'];
        $buy->ym_price = $close - $close * 0.01;
        $buy->save();

        $this->sbase--;

    }

    function addbuy($hck) {
        $close = $hck['close'];//需要买入的价格
        $up = $close + $close * 0.01;
        $down = $close - $close * 0.01;
        $list = \app\admin\model\stock\Mmlog::where('state', 0)->where("buy_price < {$up}  and  buy_price > {$down}" )->select();
        if ($list) {
            return;
        }

        $buy = new \app\admin\model\stock\Mmlog();
        $buy->buy_price = $close;
        $buy->riqi = $hck['riqi'];
        $buy->time = $hck['time'];
        $buy->ym_price = $close + $close * 0.01;
        $buy->save();

        $this->sbase++;

    }

    //http://gupiao.test/api/gupiao2/dobuy?riqi=2022-04-06
    //找到一天的买点
    //从第5个点开始看机会
    function dobuy($riqi)
    {
        $list = collection(\app\admin\model\stock\Hck::where('riqi', $riqi)->select())->toArray();

        for ($i = 0; $i < count($list); $i++) {

            $this->doT($list[$i]);
            $this->doFT($list[$i]);

            if ($i > 3) {
                $listA = [];
                $j = 0;
                foreach ($list as $t) {
                    $listA[] = $t;
                    $j++;
                    if ($j > $i) {
                        $this->isbuy($listA);
                        break;
                    }
                }
            }

        }

//        echo "<br> --ok ---";
    }

    //mmlog -- 完成正T
    function doT($hck) {
        $list = \app\admin\model\stock\Mmlog::where('state', 0)->where('ym_price', '<', $hck['close'])->where('riqi', $hck['riqi'])->select();
        foreach ($list as &$mm) {
            $mm->profit = ($hck['close'] - $mm['buy_price']) * 100;
            $mm->s_time = $hck['time'];
            $mm->r_price = $hck['close'];
            $mm->s_riqi = $hck['riqi'];
            $mm->state = 1;
            $mm->save();
            $this->sbase--;
        }
    }

    //sslog -- 完成反T
    function doFT($hck) {
        $list = \app\admin\model\stock\Sslog::where('state', 0)->where('ym_price', '>', $hck['close'])->where('riqi', $hck['riqi'])->select();
        foreach ($list as &$mm) {
            $mm->profit = ($mm['s_price'] - $hck['close']) * 100;
            $mm->b_time = $hck['time'];
            $mm->b_price = $hck['close'];
            $mm->s_riqi = $hck['riqi'];
            $mm->state = 1;
            $mm->save();
            $this->sbase++;
        }
    }

    //http://gupiao.test/api/gupiao2/lllbuy?from=2021-11-15&&to=2022-04-06
    function lllbuy($from, $to)
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        ob_end_clean();
        ob_implicit_flush();
        header('X-Accel-Buffering: no');

        $riqi = $from;
        while (true) {
            $this->dobuy($riqi);
            $riqi = date("Y-m-d", strtotime("+1 days", strtotime($riqi)));
            if ($riqi > $to) {
                break;
            }
            echo "<br>".$riqi." 持仓 ". $this->sbase;
        }
        echo "<br> --ok ---";
    }

}

// select sum(profit) from fa_stock_mmlog
// select sum(profit) from fa_stock_sslog

/*
 * 按换手率低->高排序，去除涨幅过大的
 convert(int,字段名) cast(字段名 as int)

select pctChg, code, turn, date from fa_stock_k where code like '%002371' and turn <> 0
and (cast(pctChg as decimal(10,4)) > -9 and cast(pctChg as decimal(10,4)) < 9)
and date > '2019-01-14'
order by turn ASC limit 10

geini_t()
SELECT `id`,`date`,`code`,`close`,`pctChg`,`turn` FROM `fa_stock_hsl` WHERE  (  code like '%300598' and cast(turn as decimal(10,4)) > 0
and (cast(pctChg as decimal(10,4)) > -4 and cast(pctChg as decimal(10,4)) < 4)
and date > '2018-10-10' ) ORDER BY `turn` ASC LIMIT 10


UPDATE fa_stock_hsl
SET iszt=1
WHERE cast(pctChg as decimal(10,4)) > 9.9


 */
